# working directory ---------------------------
setwd("~/Library/CloudStorage/OneDrive-UniversitédeGenève/oecd-dac-project-unige/paper-gender-targeting/2025-ddfi-isq-replication")

# packages ---------------------------
library(tidyverse)
library(lfe) # for felm()
library(stargazer)

# data ---------------------------
ry <- read_csv("2025-ddfi-isq-data-ry.csv") # recipient-year level, built including eu lines 
dry <- read_csv("2025-ddfi-isq-data-dry.csv") # donor-recipient-year level

# t1 m1-4 ---------------------------
# ry level 
# outcome: commit_gender_1_pct
# democracy measure: vdem_row_id_ry
# year fixed effects
# standard errors clustered on recipient level 
# models 1-4 in table 1 

m1 <- felm(commit_gender_1_pct ~ vdem_row_id_ry + quota_adpt_ry 
                    + log(gdppc_ry) + dependence_ry 
                    | y | 0 | r, 
                 data = ry)
summary(m1)

m2 <- felm(commit_gender_1_pct ~ vdem_row_id_ry + wbl_ry 
           + log(gdppc_ry) + dependence_ry 
           | y | 0 | r, 
           data = ry)
summary(m2)

m3 <- felm(commit_gender_1_pct ~ vdem_row_id_ry * quota_adpt_ry 
           + log(gdppc_ry) + dependence_ry 
           | y | 0 | r, 
           data = ry)
summary(m3)

m4 <- felm(commit_gender_1_pct ~ vdem_row_id_ry * wbl_ry 
           + log(gdppc_ry) + dependence_ry 
           | y | 0 | r, 
           data = ry)
summary(m4)


# t1 m5-6 ---------------------------
# dry level 
# outcome: commit_gender_1_pct
# donor and year fixed effects
# standard errors clustered on recipient level 
# models 5-6 in table 1 

m5 <- felm(commit_gender_1_pct ~ vdem_row_id_ry * quota_adpt_ry 
           + log(gdppc_ry) + dependence_ry + importance_rtod_dry + seats_dy 
           | d + y | 0 | r, 
           data = dry)
summary(m5)

m6 <- felm(commit_gender_1_pct ~ vdem_row_id_ry * wbl_ry 
           + log(gdppc_ry) + dependence_ry + importance_rtod_dry + seats_dy 
           | d + y | 0 | r, 
           data = dry)
summary(m6)


stargazer(m1, m2, m3, m4, m5, m6, 
          type = "text",
          # type = "latex",
          title = "Determinants of Gender Mainstreaming in Recipient Countries", # align = TRUE,
          dep.var.labels = "% Mainstreamed Commitments (GEPM = 1)", 
          covariate.labels = c("Democracy", "Quota", "WBL", 
                               "GDP per capita (log)", "Aid Dependence", 
                               "Importance to Donor", "Women in Donor Legislature",
                               "Democracy x Quota", "Democracy x WBL"), 
          omit.stat = c("LL", "ser", "f"), no.space = TRUE, 
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001), 
          notes = c("+ $p<0.1$; * $p<0.05$; ** $p<0.01$; *** $p<0.001$"), 
          notes.append = FALSE)